package thread.safe;

import java.util.concurrent.CountDownLatch;

/**
 * @author bc
 * @Description:
 * @date 2024/03/11
 */
@SuppressWarnings("ALL")
public class CountDownLatchTest {

    public static void main(String[] args) {

        CountDownLatch countDownLatch = new CountDownLatch(3);

        Thread t1 = new Thread(() -> {
            System.out.println("t1 start!");
            try {
                Thread.sleep(5000);

                countDownLatch.countDown();
                System.out.println("t1 end!");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t2 = new Thread(() -> {
            System.out.println("t2 start!");
            try {
                Thread.sleep(3000);

                countDownLatch.countDown();
                System.out.println("t2 end!");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread t3 = new Thread(() -> {
            System.out.println("t1 start!");
            try {
                Thread.sleep(3000);

                countDownLatch.countDown();
                System.out.println("t3 end!");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        t1.start();
        t2.start();
        t3.start();
        System.out.println("主线程启动");
        try {
            //卡住主线程
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("主线程结束");
    }
}
